<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\captcha\Captcha;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $lyId = 371; //录用 

    /**
     * 生成验证码
     */
    public function crateCode()
    {
        $captcha = new Captcha();
        $rst = $captcha->entry();
        $base64 = 'data:image/png;base64,' . base64_encode($rst->getData());
        $this->success('查询成功', $base64);
    }

    /**
     * 添加留言
     */
    public function addLevelMsg()
    {
        $name = $this->request->post("name", '');
        $contact = $this->request->post("contact", '');
        $content = $this->request->post("content", '');
        $code = $this->request->post("code", '');

        empty($name) ? $this->error('标题不能为空') : '';
        empty($contact) ? $this->error('姓名不能为空') : '';
        empty($content) ? $this->error('内容不能为空') : '';
        empty($code) ? $this->error('验证码不能为空') : '';
        $captcha = new Captcha();
        if (!$captcha->check($code)) {
            $this->error('验证码不正确');
        }
        $data = [
            'name' => $name,
            'contact' => $contact,
            'content' => $content,
            'ip' => $this->getRealIp(),
            'verify' => 0,
            'reply' => '',
            'time' => date('Y-m-d H:i:s')
        ];
        $rst = Db::name('book')->insert($data);

        $rst ? $this->success('添加成功') : $this->error('添加失败');
    }

    public function getRealIp()
    {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    /**
     * 录用公告
     */
    public function hireNotice()
    {
        $list = Db::name('content')->where('columnsname', $this->lyId)->where('lyqk', '已录用')->limit(5)->order(['rec' => 'desc', 'hot' => 'desc', 'addtime' => 'desc'])->select();
        $this->success('请求成功', $list);
    }

    public function hireNoticeList()
    {
        $pageSize = $this->request->post("pageSize");
        $pageSize =  $pageSize ? $pageSize : 10;
        $list = Db::name('content')->where('columnsname', $this->lyId)->where('lyqk', '已录用')->order(['rec' => 'desc', 'hot' => 'desc', 'addtime' => 'desc'])->paginate($pageSize);
        $this->success('请求成功', $list);
    }

    public function single()
    {
        $id = $this->request->post("id");
        $rst = Db::name('columns')->where('hide', 0)->where('id', $id)->find();
        $this->success('请求成功', $rst);
    }

    /**
     * 期刊目录
     */
    public function qkmls()
    {
        $bid = $this->request->post("bid");
        $rst = Db::name('columns')->where('hide', 0)->where('bid', $bid)->order('id', 'asc')->select();
        foreach ($rst as $k => $v) {
            $c_rst = Db::name('columns')->where('hide', 0)->where('bid', $v['id'])->order('id', 'asc')->select();
            $rst[$k]['children'] = $c_rst;
        }
        $this->success('请求成功', $rst);
    }

    /**
     * 期刊目录列表
     */
    public function qkmls_list()
    {
        $bid = $this->request->post("bid");
        $pageSize = $this->request->post("pageSize");
        $pageSize =  $pageSize ? $pageSize : 10;
        $rst = Db::name('columns')->where('hide', 0)->where('bid', $bid)->order('id', 'asc')->paginate($pageSize)->each(function ($item) {
            $c_rst = Db::name('content')->where('columnsname', $item['id'])->order('id', 'asc')->select();
            $item['children'] = $c_rst;
            return $item;
        });

        $this->success('请求成功', $rst);
    }

    /**
     * 详情
     */
    public function content_detail()
    {
        $id = $this->request->post("id");
        $rst = Db::name('content')->where('id', $id)->find();

        $columns = Db::name('columns')->where('id', $rst['columnsname'])->find();
        $rst['columns'] = $columns ? $columns : [];

        Db::name('content')->where('id', $id)->inc('hits')->update();

        $products = Db::name('products')->where('bid', $rst['id'])->find();
        $rst['products'] = $products ? $products : [];
        $this->success('请求成功', $rst);
    }

    /**
     * 直查内容
     */
    public function columns()
    {
        $id = $this->request->post("id");
        $isxj = $this->request->post("isxj", '');
        $pageSize = $this->request->post("pageSize");
        $pageSize =  $pageSize ? $pageSize : 10;
        $rst = Db::name('content')->where('columnsname', $id);
        if ($isxj) {
            $rst = $rst->where(function($query){
                $query->where('hot', 1);
                // $query->whereOr('rec', 1);
            });
        }
        $rst = $rst->order('id', 'desc')->paginate($pageSize)->each(function ($item) {
            $t_time = $item['addtime'];
            $item['addtime'] = date('Y-m-d', strtotime($t_time));
            $hr = Db::name('hr')->where('bid', $item['id'])->find();
            $item['hr'] = $hr ? $hr : [];
            $glqstr = '';
            if (!empty($item['glqs_id'])) {
                $qsInfo = Db::name('columns')->where('id', $item['glqs_id'])->find();
                $qsYearInfo = Db::name('columns')->where('id', $qsInfo['bid'])->find();
                $glqstr = $qsYearInfo['columnsname'].$qsInfo['columnsname'];
            }
            $item['glqstr'] = $glqstr;
            return $item;
        }); 
        $this->success('请求成功', $rst);
    }

    /**
     * 留言列表
     */
    public function level_msg_list()
    {
        $bid = $this->request->post("bid");
        $pageSize = $this->request->post("pageSize");
        $pageSize =  $pageSize ? $pageSize : 10;
        $rst = Db::name('book')->order('id', 'desc')->where('verify', 1)->paginate($pageSize);

        $this->success('请求成功', $rst);
    }

    /**
     * 文件检索 筛选项
     */
    public  function searchItem()
    {
        $bid = $this->request->post("bid");
        $rst = Db::name('columns')->where('hide', 0)->where('bid', $bid)->select();
        $this->success('请求成功', $rst);
    }

    /**
     * 文件检索列表
     */
    public function searchList()
    {
        $fid = $this->request->post("fid", '');
        $sid = $this->request->post("sid", '');
        $tid = $this->request->post("tid", '');
        $keyword = $this->request->post("keyword", '');
        empty($fid) ? $this->error('请选择年份') : '';
        empty($sid) ? $this->error('请选择期数') : '';
        empty($tid) ? $this->error('请选择栏目') : '';
        $where = [];
        $t_columnsname = '';
        $where = [];
        if ($keyword) {
            $where['title'] = ['like', '%' . $keyword . '%'];
        }
        if ($tid) {
            $t_columnsname = $tid;
        }
        
        // if (!$tid && $sid) {
        //     $s_columnsname =  Db::name('columns')->where('hide', 0)->where('bid', $sid)->field('id')->select();
        //     $s_columnsname = !empty($s_columnsname) ? collection($s_columnsname)->toArray() : [];
        //     $t_columnsname = array_column($s_columnsname, 'id');   
        // }

        // if (!$tid && !$sid && $fid) {

        //     $f_columnsname =  Db::name('columns')->where('hide', 0)->where('bid', $fid)->field('id')->select();
        //     $f_columnsname = !empty($f_columnsname) ? collection($f_columnsname)->toArray() : [];
        //     $f_columnsname = array_column($f_columnsname, 'id');


        //     $s_columnsname =  Db::name('columns')->where('hide', 0)->where('bid', 'in',$f_columnsname)->field('id')->select();
        //     $s_columnsname = !empty($s_columnsname) ? collection($s_columnsname)->toArray() : [];
        //     $t_columnsname = array_column($s_columnsname, 'id'); 
        // }
        if ($t_columnsname) {
            $where['columnsname'] = ['in', $t_columnsname];
        }
        
        $pageSize = $this->request->post("pageSize");
        $pageSize =  $pageSize ? $pageSize : 10;
        $rst = Db::name('content')->where($where)->paginate($pageSize);
        $this->success('请求成功', $rst);
    }
}
